#!/usr/bin//env perl

use CGI;
use File::Basename;

print "Job Control\n";

$cfg = dirname("$0")."/config2.txt";
$dir = dirname("$0");

# Open Config file
open(FILE, "<$cfg") or die("Cannot open config file");

print "#########  DATA  #########\n";
print "CFG: $cfg\n";
print "DIR: $dir\n";
print "##########################\n\n";
if (eof(FILE)){
	print "Archivo de configuracion vacio\n";
	exit 1;
}

$dot = "";
$result = '0';


#if ($1 == "-f"){
#	print "Deleting job control log files...\n\n";
#	system "rm *.jclog 2> /dev/null";
#}

while (!eof(FILE)) {
	chomp($cmd = <FILE>);
	chomp($in = <FILE>);
	chomp($out = <FILE>);
	chomp($log = <FILE>);
	chomp($err = <FILE>);
	chomp($dot = <FILE>);

	$jclog = "";
	if ($result eq '0'){
		$jclog = `cat $dir/jclog/$cmd.jclog 2> /dev/null`;
		#print "Log file contents for command $cmd = $jclog\n ";
		if ($jclog eq "0\n"){
			print "Skipping $cmd because it was correctly executed before\n";
		} else {
			print "Executing $dir/$cmd $in $out $log $err\n";
			system "$dir/$cmd $in $out $log $err";
			$result = $?;
			print "----> Result |$result|\n";
			chomp($result);
			system "echo $result > $dir/jclog/$cmd.jclog";
		}
	} else {
		print "Skipping $cmd $in $out $log $err\n";
	}
}

close(FILE);
